package com.lily.aidouzixun.bean;

import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

import com.alibaba.fastjson.JSONObject;
import com.lily.aidouzixun.util.DataConverter;
import com.lily.aidouzixun.util.MyConstant;
import com.lily.aidouzixun.util.OKHttpClientManager;
import com.lily.aidouzixun.util.Validator;
import com.squareup.okhttp.Request;

import java.util.Map;

/**
 * 用户信息类
 */
public class UserInfo implements Parcelable{
    private static final String TAG = "UserInfo";

    /**
     * 用户类型
     * 1-用户名密码
     * 2-QQ
     * 3-手机短信验证
     */
    public interface UserType {
        /**
         * 用户名密码
         */
        int USERNAME_PWD = 1;
        /**
         * QQ
         */
        int QQ = 2;
        /**
         * 手机短信
         */
        int PHOME_SMS = 3;
    }

    /**json数据解析的key,对应服务端数据库中值**/
    private static final String URID = "URID";
    private static final String OPEND_ID = "OpenId";
    private static final String LOGIN_PHONE = "LoginPhone";
    private static final String USERNAME = "UserName";
    private static final String PASSWORD = "password";
    private static final String NICKNAME = "NickName";
    private static final String QQ_NICKNAME = "nickname";
    private static final String AVATARURL = "Avatar";
    private static final String QQ_AVATARURL = "figureurl_qq_2";
    private static final String EMAIL = "Email";
    private static final String PHONE = "Phone";
    private static final String LOGIN_FROM = "LoginFrom";
    private static final String POINT = "Point";

    private String urid;
    private String openId;  //QQ登录成功返回的唯一标识
    private String username;
    private String password;
    private String nickname;
    private String avatarUrl;
    private String email;
    private String phone;
    private String loginPhone;
    private int loginFrom;  //用户类型；1-用户名密码；2-QQ;3-手机
    private String brithday;
    private int sex;    //0-未知；1-男；2-女
    private String address;
    private String selfsign;
    private int point;

    public int getPoint() {
        return point;
    }

    public void setPoint(int point) {
        this.point = point;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUrid() {
        return urid;
    }

    public void setUrid(String urid) {
        this.urid = urid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getOpenId() {
        return openId;
    }

    public void setOpenId(String openId) {
        this.openId = openId;
    }

    public int getLoginFrom() {
        return loginFrom;
    }

    public void setLoginFrom(int loginFrom) {
        this.loginFrom = loginFrom;
    }

    public String getLoginPhone() {
        return loginPhone;
    }

    public void setLoginPhone(String loginPhone) {
        this.loginPhone = loginPhone;
    }

    /**
     * 解析数据
     * @param jsonObj
     * @return 用户信息对象
     */
    public static UserInfo parseInfo(JSONObject jsonObj) {
        UserInfo userInfo = new UserInfo();
        if (jsonObj.has(URID)) {
            userInfo.setUrid(jsonObj.optString(URID));
        }
        if (jsonObj.has(OPEND_ID)) {
            userInfo.setOpenId(jsonObj.optString(OPEND_ID));
        }
        if (jsonObj.has(USERNAME)) {
            userInfo.setUsername(jsonObj.optString(USERNAME));
        }
        if (jsonObj.has(PASSWORD)) {
            userInfo.setPassword(jsonObj.optString(PASSWORD));
        }
        if (jsonObj.has(NICKNAME)) {
            userInfo.setNickname(jsonObj.optString(NICKNAME));
        }
        if (jsonObj.has(AVATARURL)) {   //把头像写到本地
            userInfo.setAvatarUrl(jsonObj.optString(AVATARURL));
            OKHttpClientManager.downImgAysn(userInfo.getAvatarUrl(), new OKHttpClientManager.ResultCallback<Object>() {
                @Override
                public void onResponse(Object response) {
                    Bitmap bitmap = (Bitmap) response;
                    DataConverter.getFilePathByBitmap(bitmap, MyConstant.AVATAR_PATH, MyConstant.DataType.USERCENTER_INFO);
                }

                @Override
                public void onError(Request request, Exception exception) {

                }
            });
        }
        if (jsonObj.has(EMAIL)) {
            userInfo.setEmail(jsonObj.optString(EMAIL));
        }
        if (jsonObj.has(PHONE)) {
            userInfo.setPhone(jsonObj.optString(PHONE));
        }
        if (jsonObj.has(LOGIN_PHONE)) {
            userInfo.setLoginPhone(jsonObj.optString(LOGIN_PHONE));
        }
        if (jsonObj.has(LOGIN_FROM)) {
            userInfo.setLoginFrom(jsonObj.optInt(LOGIN_FROM));
        }
        if (jsonObj.has(POINT)) {
            userInfo.setPoint(jsonObj.optInt(POINT));
        }

        return userInfo;
    }
    /**
     * 解析QQ登录的数据
     * @param jsonObj
     * {
     *"is_yellow_year_vip": "0",
     *"ret": 0,
     *"figureurl_qq_1": "http://q.qlogo.cn/qqapp/222222/94EE5CDFFF4834FF1E83B498849728F5/40",
     *"figureurl_qq_2": "http://q.qlogo.cn/qqapp/222222/94EE5CDFFF4834FF1E83B498849728F5/100",
     *"nickname": "",
     *"yellow_vip_level": "0",
     *"is_lost": 0,
     *"msg": "",
     *"city": "浦东新区",
     *"figureurl_1": "http://qzapp.qlogo.cn/qzapp/222222/94EE5CDFFF4834FF1E83B498849728F5/50",
     *"vip": "0",
     *"level": "0",
     *"figureurl_2": "http://qzapp.qlogo.cn/qzapp/222222/94EE5CDFFF4834FF1E83B498849728F5/100",
     *"province": "上海",
     *"is_yellow_vip": "0",
     *"gender": "男",
     *"figureurl": "http://qzapp.qlogo.cn/qzapp/222222/94EE5CDFFF4834FF1E83B498849728F5/30"
     *}
     * @return 用户信息对象
     */
    public static UserInfo parseQQUser(org.json.JSONObject jsonObj) {
        UserInfo userInfo = new UserInfo();
        if (jsonObj.has(QQ_NICKNAME)) {
            userInfo.setNickname(jsonObj.optString(QQ_NICKNAME));
        }
        if (jsonObj.has(QQ_AVATARURL)) {
            userInfo.setAvatarUrl(jsonObj.optString(QQ_AVATARURL));
        }
        return userInfo;
    }

    /**
     * 解析友盟QQ的方法
     * is_yellow_year_vip=0
     * vip=0
     * level=0
     * province=上海
     * yellow_vip_level=0
     * is_yellow_vip=0
     * gender=男
     * openid=B0F1058E51A117E9820D3EB7D00E0B8E
     * screen_name=爱豆咨询
     * msg=
     * profile_image_url=http://q.qlogo.cn/qqapp/1105059559/B0F1058E51A117E9820D3EB7D00E0B8E/100
     * city=浦东新区
     * @param umengQQUser
     * @return
     */
    public static UserInfo parseUmengQQUser(Map<String, Object> umengQQUser) {
        UserInfo userInfo = new UserInfo();
        String openId = (String) umengQQUser.get("openid");
        if (Validator.isEffective(openId)) {
            userInfo.setOpenId(openId);
        }
        String nickName = (String) umengQQUser.get("screen_name");
        if (Validator.isEffective(nickName)) {
            userInfo.setNickname(nickName);
        }
        String avatarUrl = (String) umengQQUser.get("profile_image_url");
        if (Validator.isEffective(avatarUrl)) {
            userInfo.setAvatarUrl(avatarUrl);
        }
        return userInfo;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "avatarUrl='" + avatarUrl + '\'' +
                ", urid='" + urid + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }

    /**
     * 序列化实体类
     */
    public static final Parcelable.Creator<UserInfo> CREATOR = new Creator<UserInfo>() {
        @Override
        public UserInfo createFromParcel(Parcel parcel) {
            UserInfo userInfo = new UserInfo();
            userInfo.urid = parcel.readString();
            userInfo.username = parcel.readString();
            userInfo.password = parcel.readString();
            userInfo.nickname = parcel.readString();
            userInfo.avatarUrl = parcel.readString();
            userInfo.email = parcel.readString();
            userInfo.phone = parcel.readString();
            userInfo.openId = parcel.readString();
            userInfo.loginPhone = parcel.readString();
            userInfo.loginFrom = parcel.readInt();
            userInfo.point = parcel.readInt();
            return userInfo;
        }

        @Override
        public UserInfo[] newArray(int i) {
            return new UserInfo[i];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    /**
     * 把实体类写到Parcel中
     * @param parcel
     * @param i
     */
    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(urid);
        parcel.writeString(openId);
        parcel.writeString(username);
        parcel.writeString(password);
        parcel.writeString(nickname);
        parcel.writeString(avatarUrl);
        parcel.writeString(email);
        parcel.writeString(phone);
        parcel.writeString(loginPhone);
        parcel.writeInt(loginFrom);
        parcel.writeInt(point);
    }

}
